#include "SemanticNodeCreateArray.h"

SemanticNodeCreateArray::SemanticNodeCreateArray(const SyntaxTreeNode *createArrayNode, SemanticNode *parent) : SemanticNode(parent), ValueType(getNextUniqueID()) {
 Gr_Value__new_DataType_ArrayDefinitionsSize_Initialisation;
	SyntaxTreeNode *varType = createArrayNode->Nodes->GetData(1);
	setType(varType, createArrayNode->Nodes->GetData(2)->Nodes->GetCount());
	if (createArrayNode->Nodes->GetData(2)->Nodes->GetCount() > 0) {
	 for (int x = 0; x < createArrayNode->Nodes->GetData(2)->Nodes->GetCount(); ++x) {
	 	dimensionExpressions.push_back(createArrayNode->Nodes->GetData(2)->Nodes->GetData(x)->Nodes->GetData(1));
	 }
	}
	extractInitializers(createArrayNode->Nodes->GetData(3));
}
/**
 *  @fn	void SemanticNodeCreateArray::getDimensionExpressions(void) const
 * 
 *  @brief	.
 * 
 *  @author	Nikola Novak.
 *  @date	6/22/2009.
 */
std::vector<const SyntaxTreeNode *> SemanticNodeCreateArray::getDimensionExpressions(void) const {
	return dimensionExpressions;
}
/**
 *  @fn	void SemanticNodeCreateArray::getInitializers(void) const
 * 
 *  @brief	.
 * 
 *  @author	Nikola Novak.
 *  @date	6/22/2009.
 */
std::vector<const SyntaxTreeNode *> SemanticNodeCreateArray::getInitializationExpressions(void) const {
	return initializationExpressions;
}
/**
 *  @fn	void SemanticNodeCreateArray::extractInitializers(const SyntaxTreeNode *initializationNode)
 *  	
 * 
 *  @brief	.
 * 
 *  @author	Nikola Novak.
 *  @date	6/22/2009.
 * 
 *  @param	initializationNode	.
 */
void SemanticNodeCreateArray::extractInitializers(const SyntaxTreeNode *initializationNode) {
	if (initializationNode->Production == Gr_InitialValue__Expression) initializationExpressions.push_back(initializationNode->Nodes->GetData(0));
	else {
	 for (int x = 0; x < initializationNode->Nodes->GetCount(); ++x) extractInitializers(initializationNode->Nodes->GetData(x));
	}
}
/**
 *  @fn	void SemanticNodeCreateArray::addDimension(SemanticNodeExpression *dimension)
 * 
 *  @brief	.
 * 
 *  @author	Nikola Novak.
 *  @date	6/22/2009.
 * 
 *  @param	dimension	.
 */
void SemanticNodeCreateArray::addDimension(SemanticNodeExpression *dimension) {
	dimensions.push_back(dimension);
}
/**
 *  @fn	void SemanticNodeCreateArray::addInitializer(SemanticNodeExpression *initializer)
 * 
 *  @brief	.
 * 
 *  @author	Nikola Novak.
 *  @date	6/22/2009.
 * 
 *  @param	initializer	.
 */
void SemanticNodeCreateArray::addInitializer(SemanticNodeExpression *initializer) {
	initializers.push_back(initializer);
}
/**
 *  @fn	std::string SemanticNodeCreateArray::toDebugString(int tab)
 * 
 *  @brief	.
 * 
 *  @author	Nikola Novak.
 *  @date	6/22/2009.
 * 
 *  @param	tab	.
 * 
 *  @return	.
 */
std::string SemanticNodeCreateArray::toDebugString(int tab) {
	std::ostringstream res;
	for (int i = 0; i < tab; i++) res << DEBUG_STRING_RAZMAK;
	res << "new " << getTypeDebugString();
	if (initializers.size() > 0) {
		res << " {" << std::endl;
	 for (std::vector<SemanticNodeExpression *>::iterator e = initializers.begin(); e != initializers.end(); ++e ) {
	 	res << ((SemanticNode *)(*e))->toDebugString(tab + 1);
	 }
		for (int i = 0; i < tab; i++) res << DEBUG_STRING_RAZMAK;
		res << "}" << std::endl;
	}
	return res.str();
}
